//创建用户相关的小仓库
import { defineStore } from "pinia";
//引入接口
import { reqLogin,reqUserAll,reqUserList,reqUserAdd,reqUserById,reqUserUpdate, reqUserDelete} from "@/api/user";
//引入数据类型
import type { loginFormData, loginResponseData, userInfoData 
    ,userListResponseData,userListFormData,userAllResponseData
} from "@/api/user/type";
//state返回对象类型
import type { UserState } from "./types/types";


let useUserStore=defineStore("User",{
    state:():UserState=>{
        return {
            token:localStorage.getItem('b0445-vue3-token')||"",
            user:JSON.parse((localStorage.getItem('VUE3-USER') as string) || "{}"),
            users:[]
        }
    },
    actions:{
        //用户登录方法
        async userLogin(data:loginFormData){
            // 调用api里面的真正发送请求的方法，实现登录逻辑
            let result:loginResponseData=await reqLogin(data);

            if(result.status==0){
                this.token=result.data?.token as string
                localStorage.setItem("b0445-vue3-token",this.token)
                //@ts-ignore
                this.user=(result.data?.user as userInfoData)
                localStorage.setItem('VUE3-USER',(JSON.stringify(this.user) as string))
                return "ok"
            }
            else{
                return Promise.reject(new Error(result.msg))
            }
        },
        removeUser(){
            this.user=null;
            localStorage.removeItem("VUE3-USER")
            localStorage.removeItem("b0445-vue3-token")
        },
        async getUserAll(){
            let result:userAllResponseData=await reqUserAll();
            if(result.status==0){
                this.users=(result.data as userInfoData[])
                return "ok"
            }
            else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async getUserList(data:userListFormData){
            let result:userListResponseData=await reqUserList(data);
            if(result.status==0){
                //@ts-ignore
                this.users=(result.data?.data as userInfoData[])
                //当前async函数返回一个成功的promise
                return result.data
            }
            else{
                return Promise.reject(new Error(result.msg))
            }
        },
         async addUser(data:userInfoData){
            let result:loginResponseData=await reqUserAdd(data);
            if(result.status==0){
                this.getUserAll()
                return "ok"
            }
            else{
                return Promise.reject(new Error(result.msg))
            }
        },
         async getUserById(_id:string){
            let result:loginResponseData=await reqUserById(_id);
            if(result.status==0){
                return result.data
            }
            else{
                return Promise.reject(new Error(result.msg))
            }
        },
        //更新
         async updateUser(data:userInfoData){
            let result:loginResponseData=await reqUserUpdate(data);
            if(result.status==0){
                this.getUserAll()
                return "ok"
            }
            else{
                return Promise.reject(new Error(result.msg))
            }
        },
        //删除
        async deleteUser(userId:string){
            let result:loginResponseData=await reqUserDelete(userId);
            if(result.status==0){
                this.getUserAll()
                return "ok"
            }
            else{
                return Promise.reject(new Error(result.msg))
            }
        }
    },

    getters:{

    }
})
export default useUserStore